<template>
<div>
  <el-row>
    <el-col>
      <el-row type="flex" class="row-bg" justify="space-around">
      <el-col :span="6">
      <div>
        <span style="font-size:12px">按活动ID查询</span>
        <el-select v-model="optIDVal" placeholder="请选择">
        <el-option v-for="item in optionsID" :key="item.value" :label="item.label" :value="item.value">
        </el-option>
        </el-select>
      </div>
      </el-col>
      <el-col :span="6">
        <div>
          <span style="font-size:12px">按活动名称查询</span>
          <el-select v-model="optActNameVal" placeholder="请选择">
          <el-option v-for="item in optionsActName" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
          </el-select>
        </div>
      </el-col>
      <el-col :span="6">
        <div>
          <span style="font-size:12px">按活动时间查询</span>
          <el-select v-model="optDateVal" placeholder="请选择">
          <el-option v-for="item in optionsDate" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
          </el-select>
        </div>
      </el-col>
      </el-row>
      
      <br />
      <div style="text-align:left">
        <el-button type = "primary">查询</el-button>
        <el-button type = "primary">取消</el-button>
        <el-button type = "primary">清空</el-button>
      </div>
      <br />
      <div style="text-align:left">
        <el-button type = "primary">添加</el-button>
        <el-button type = "primary">删除</el-button>
        <el-button type = "primary">保存</el-button>
        <el-button type = "primary">取消</el-button>
      </div>
    <el-table size="mini" :data="view_info.tabledata" border style="width: 100%" highlight-current-row>
      <!-- <el-table-column type="index"></el-table-column> -->
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column v-for="(v,i) in view_info.columns"  :key='i' :prop="v.field" :label="v.title" :width="v.width"  :show-overflow-tooltip="true">
          <template slot-scope="scope">
              <span v-if="scope.row.isSet">
                  <el-input size="small" placeholder="请输入内容" v-model="view_info.sel[v.field]">
                  </el-input>
              </span>
              <span v-else>{{scope.row[v.field]}}</span>
          </template>
      </el-table-column>
      <el-table-column label="操作" width="100">
          <template slot-scope="scope">
              <span class="el-tag el-tag--info el-tag--mini" style="cursor: pointer;" @click="changeTableData(scope.row,scope.$index,true)">
                  {{scope.row.isSet?'保存':"修改"}}
              </span>
              <span v-if="!scope.row.isSet" class="el-tag el-tag--danger el-tag--mini" style="cursor: pointer;"  @click="delTableData(scope.row,scope.$index)">
                  删除
              </span>
              <span v-else class="el-tag  el-tag--mini" style="cursor: pointer;" @click="changeTableData(scope.row,scope.$index,false)">
                  取消
              </span>
          </template>
      </el-table-column>
  </el-table>
  </el-col>
      <el-col>
          <div class="el-table-add-row" style="width: 99.2%;" @click="addTableData()"><span>+ 添加</span></div>
      </el-col>
  </el-row>
</div>
</template>
<script>
export default {
  data () {
    return {
      view_info: {
        sel: null, // 选中行
        columns: [
        { field: 'id', title: '活动 ID', width: 70 },
        { field: 'actname', title: '活动名称', width: 80 },
        { field: 'actdate', title: '活动时间', width: 80 },
        { field: 'content', title: '活动内容', width: 80 },
        { field: 'customerinfo', title: '客户信息', width: 80 },
        { field: 'customersex', title: '客户性别', width: 80 },
        { field: 'customernum', title: '客户电话', width: 80 },
        { field: 'address', title: '活动地点', width: 80 },
        { field: 'enddate', title: '活动结束时间', width: 100 }],
        tabledata: []
      },
      optionsID: [{
          value: '1',
          label: '活动id1'
        }, {
          value: '2',
          label: '活动id2'
        }],
        optIDVal: '',
      optionsActName: [{
          value: 'name1',
          label: 'name1'
        }, {
          value: 'name2',
          label: 'name2'
        }],
        optActNameVal: '',
        optionsDate: [{
          value: 'd1',
          label: '日期1'
        }, {
          value: 'd2',
          label: '日期2'
        }],
        optDateVal: ''
    }
  },
  methods: {
    addTableData () {
        for (let i of this.view_info.tabledata) {
            if (i.isSet) return this.$message.warning('请先保存当前编辑项')
        }
        let j = { id: 0, 'type': '', 'addport': '', 'user': '', 'pwd': '', 'info': '', 'isSet': true, '_temporary': true }
        this.view_info.tabledata.push(j)
        this.view_info.sel = JSON.parse(JSON.stringify(j))
    },
    changeTableData (row, index, cg) {
      // 点击修改 判断是否已经保存所有操作
      for (let i of this.view_info.tabledata) {
          if (i.isSet && i.id !== row.id) {
              this.$message.warning('请先保存当前编辑项')
              return false
          }
      }
      // 是否是取消操作
      if (!cg) {
          if (!this.view_info.sel.id) this.view_info.tabledata.splice(index, 1)
          return (row.isSet = !row.isSet)
      }
      // 提交数据
      if (row.isSet) {
          // sql insert data
          let data = JSON.parse(JSON.stringify(this.view_info.sel))
          for (let k in data) row[k] = data[k]
          this.$message({type: 'success', message: '保存成功!'})
          row.isSet = false
      } else {
          this.view_info.sel = JSON.parse(JSON.stringify(row))
          row.isSet = true
      }
    },
    delTableData (row, index) {
      this.view_info.tabledata.splice(index, 1)
    }
}
}
</script>
<style>
.el-table-add-row {
    margin-top: 10px;
    width: 100%;
    height: 34px;
    border: 1px dashed #c1c1cd;
    border-radius: 3px;
    cursor: pointer;
    justify-content: center;
    display: flex;
    line-height: 34px;
}
.el-table .cell, .el-table th {
    height: 35px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    line-height: 15px;
    user-select: text;
}
.el-select {
    display: inline-block;
    position: relative;
    width: 120px;
}
</style>
